/*
 * DroptargetColorOptionsPanel.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.settings.gui;

import java.awt.Color;
import java.util.ResourceBundle;
import javax.swing.JColorChooser;
import org.tiberiumlabs.lailaps.Mediator;
import org.tiberiumlabs.lailaps.gui.GUIBuilder;
import org.tiberiumlabs.lailaps.gui.droptarget.BackgroundPanel;
import org.tiberiumlabs.lailaps.settings.InterfaceSettingsImpl;
import org.tiberiumlabs.lailaps.settings.Settings;

/**
 *
 * @author <a href="mailto:beast.tiberiumlabs@gmail.com">Beast</a>
 */
public class ApperaceOptionsPanel extends javax.swing.JPanel implements OptionPanel {

    private InterfaceSettingsImpl settings = Settings.getEditableInterfaceSettings();
    private ResourceBundle bundle = ResourceBundle.getBundle("org/tiberiumlabs/lailaps/resources/messages/settings_apperance");
    private boolean firstTime;

    /** Creates new form DroptargetColorOptionsPanel */
    public ApperaceOptionsPanel(boolean firstTime) {
        initComponents();
        this.firstTime = firstTime;
        c1 = settings.getDroptargetColor1();
        c2 = settings.getDroptargetColor2();
        elements = settings.getDroptargetElementsColor();
        color1Field.setBackground(c1);
        color2Field.setBackground(c2);
        elementField.setBackground(elements);
        if (settings.getDroptargetDirection() == BackgroundPanel.HORIZONTAL) {
            horizontalRadioButton.setSelected(true);
        } else {
            verticalRadioButton.setSelected(true);
        }

        showDroptargetCheckBox.setSelected(settings.isShowDropTargetOnStart());
        showMainWndCheckBox.setSelected(settings.isShowMainWindowOnStart());
    }

    private JColorChooser colorChooser = new JColorChooser();
    private Color c1;
    private Color c2;
    private Color elements;


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        orientationGroup = new javax.swing.ButtonGroup();
        orientationPanel = new javax.swing.JPanel();
        verticalRadioButton = new javax.swing.JRadioButton();
        horizontalRadioButton = new javax.swing.JRadioButton();
        colorsPanel = new javax.swing.JPanel();
        color1Label = new javax.swing.JLabel();
        color2Label = new javax.swing.JLabel();
        color1Field = new javax.swing.JTextField();
        color2Field = new javax.swing.JTextField();
        color1Button = new javax.swing.JButton();
        color2Button = new javax.swing.JButton();
        elementsLabel = new javax.swing.JLabel();
        elementField = new javax.swing.JTextField();
        elementButton = new javax.swing.JButton();
        interfacePanel = new javax.swing.JPanel();
        showDroptargetCheckBox = new javax.swing.JCheckBox();
        showMainWndCheckBox = new javax.swing.JCheckBox();

        orientationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("orientation.panel.title"))); // NOI18N

        orientationGroup.add(verticalRadioButton);
        verticalRadioButton.setFont(new java.awt.Font("Dialog", 0, 12));
        verticalRadioButton.setSelected(true);
        verticalRadioButton.setText(bundle.getString("vertical.title")); // NOI18N
        verticalRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        verticalRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        orientationGroup.add(horizontalRadioButton);
        horizontalRadioButton.setFont(new java.awt.Font("Dialog", 0, 12));
        horizontalRadioButton.setText(bundle.getString("horizontal.title")); // NOI18N
        horizontalRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        horizontalRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout orientationPanelLayout = new javax.swing.GroupLayout(orientationPanel);
        orientationPanel.setLayout(orientationPanelLayout);
        orientationPanelLayout.setHorizontalGroup(
            orientationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(orientationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(orientationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(horizontalRadioButton)
                    .addComponent(verticalRadioButton))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        orientationPanelLayout.setVerticalGroup(
            orientationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(orientationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(verticalRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(horizontalRadioButton)
                .addContainerGap(47, Short.MAX_VALUE))
        );

        colorsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("colors.panel.title"))); // NOI18N

        color1Label.setFont(new java.awt.Font("Dialog", 0, 12));
        color1Label.setText(bundle.getString("color1.label")); // NOI18N

        color2Label.setFont(new java.awt.Font("Dialog", 0, 12));
        color2Label.setText(bundle.getString("color2.label")); // NOI18N

        color1Field.setEditable(false);

        color2Field.setEditable(false);

        color1Button.setFont(new java.awt.Font("Dialog", 0, 12));
        color1Button.setText(bundle.getString("change.button.text")); // NOI18N
        color1Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                color1ButtonActionPerformed(evt);
            }
        });

        color2Button.setFont(new java.awt.Font("Dialog", 0, 12));
        color2Button.setText(bundle.getString("change.button.text")); // NOI18N
        color2Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                color2ButtonActionPerformed(evt);
            }
        });

        elementsLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        elementsLabel.setText(bundle.getString("elements.label")); // NOI18N

        elementField.setEditable(false);

        elementButton.setFont(new java.awt.Font("Dialog", 0, 12));
        elementButton.setText(bundle.getString("change.button.text")); // NOI18N
        elementButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                elementButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout colorsPanelLayout = new javax.swing.GroupLayout(colorsPanel);
        colorsPanel.setLayout(colorsPanelLayout);
        colorsPanelLayout.setHorizontalGroup(
            colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(colorsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(color1Label)
                    .addComponent(color2Label)
                    .addComponent(elementsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(elementField, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                    .addComponent(color2Field, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                    .addComponent(color1Field, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(color1Button)
                    .addComponent(color2Button)
                    .addComponent(elementButton))
                .addContainerGap())
        );
        colorsPanelLayout.setVerticalGroup(
            colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(colorsPanelLayout.createSequentialGroup()
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(color1Label)
                    .addComponent(color1Button)
                    .addComponent(color1Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(color2Label)
                    .addComponent(color2Button)
                    .addComponent(color2Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(colorsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(elementsLabel)
                    .addComponent(elementButton)
                    .addComponent(elementField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        interfacePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("interfacePanel"))); // NOI18N

        showDroptargetCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        showDroptargetCheckBox.setText(bundle.getString("showDroptargetCheckBox")); // NOI18N
        showDroptargetCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        showDroptargetCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        showMainWndCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        showMainWndCheckBox.setText(bundle.getString("showMainWndCheckBox")); // NOI18N
        showMainWndCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        showMainWndCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout interfacePanelLayout = new javax.swing.GroupLayout(interfacePanel);
        interfacePanel.setLayout(interfacePanelLayout);
        interfacePanelLayout.setHorizontalGroup(
            interfacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(interfacePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(interfacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(showMainWndCheckBox)
                    .addComponent(showDroptargetCheckBox))
                .addContainerGap(143, Short.MAX_VALUE))
        );
        interfacePanelLayout.setVerticalGroup(
            interfacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(interfacePanelLayout.createSequentialGroup()
                .addComponent(showMainWndCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(showDroptargetCheckBox)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(colorsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(orientationPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(interfacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(orientationPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(colorsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(interfacePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(118, 118, 118))
        );
    }// </editor-fold>//GEN-END:initComponents

private void elementButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_elementButtonActionPerformed
        Color ret = colorChooser.showDialog(this,"Change Elements color", elements);
        if (ret != null) {
            elements = ret;
        }
        elementField.setBackground(elements);
}//GEN-LAST:event_elementButtonActionPerformed
    
    private void color2ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_color2ButtonActionPerformed
        Color ret = colorChooser.showDialog(this,"Change color # 2", c2);
        if (ret != null) {
            c2 = ret;
        }
        color2Field.setBackground(c2);
    }//GEN-LAST:event_color2ButtonActionPerformed
    
    private void color1ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_color1ButtonActionPerformed
        Color ret = colorChooser.showDialog(this,"Change color # 1", c1);
        if (ret != null) {
            c1 = ret;
        }
        color1Field.setBackground(c1);
    }//GEN-LAST:event_color1ButtonActionPerformed
    
    public void applyAction() {
        int orientation;
        if (verticalRadioButton.isSelected()) {
            orientation = BackgroundPanel.VERTICAL;
        } else {
            orientation = BackgroundPanel.HORIZONTAL;
        }
        
        if (!firstTime) {
            GUIBuilder guiBuilder = Mediator.getMediator().getGuiBuilder();
            if  (guiBuilder.isDropTargetVisible()) {
            Color c1out = new Color(c1.getRed(), c1.getGreen(), c1.getBlue());
            Color c2out = new Color(c2.getRed(), c2.getGreen(), c2.getBlue());
            
            Mediator.getMediator().getGuiBuilder().getDropTarget().setNewBackground(c1out, c2out, elements, orientation);
            }
        }
        
        settings.setDroptargetColor1(c1);
        settings.setDroptargetColor2(c2);
        settings.setDroptargetElementsColor(elements);
        settings.setDroptargetDirection(orientation);
        settings.setShowDropTargetOnStart(showDroptargetCheckBox.isSelected());
        settings.setShowMainWindowOnStart(showMainWndCheckBox.isSelected());
        
        Settings.saveInterfaceSettings();
    }
    
    public void okAction() {
        applyAction();
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton color1Button;
    private javax.swing.JTextField color1Field;
    private javax.swing.JLabel color1Label;
    private javax.swing.JButton color2Button;
    private javax.swing.JTextField color2Field;
    private javax.swing.JLabel color2Label;
    private javax.swing.JPanel colorsPanel;
    private javax.swing.JButton elementButton;
    private javax.swing.JTextField elementField;
    private javax.swing.JLabel elementsLabel;
    private javax.swing.JRadioButton horizontalRadioButton;
    private javax.swing.JPanel interfacePanel;
    private javax.swing.ButtonGroup orientationGroup;
    private javax.swing.JPanel orientationPanel;
    private javax.swing.JCheckBox showDroptargetCheckBox;
    private javax.swing.JCheckBox showMainWndCheckBox;
    private javax.swing.JRadioButton verticalRadioButton;
    // End of variables declaration//GEN-END:variables
    
}
